/*
 * $Author: isan.wong@gmail.com $
 * $Date: 2011-07-29 01:03:06 +0000 (Fri, 29 Jul 2011) $
 * $Rev: 13 $
 * $URL: http://capitalbio-smd.googlecode.com/svn/src/com/capitalbio/smd/base/Project.java $
 */

package com.capitalbio.smd.base;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 项目
 * @author Wang Lei
 */
public class Project {
	protected Log log = LogFactory.getLog(this.getClass());
	
	private static int untitledIndex = 1;
	public static final String PROJECT_FILE_EXTENTION = "smd";
	
	private boolean statusChanged = false;
	private String name;
	private File file;
	private List<Sample> samples;
	private Properties properties;
	
	/**
	 * 新建一个项目
	 */
	public Project() {
		samples = new ArrayList<Sample>();
	}

	/**
	 * 新建一个项目
	 * @param name 项目名称
	 */
	public Project(String name) {
		this.name = name;
		samples = new ArrayList<Sample>();
	}
	
	/**
	 * 载入项目
	 * @param file 项目文件
	 */
	public static Project load(File file) {
		Project project = new Project();
		
		List<Sample> samples = new ArrayList<Sample>();
		project.setSamples(samples);
		
		return project;
	}
	
	/**
	 * 保存项目
	 */
	public boolean save() {
		try {
			FileWriter writer = new FileWriter(this.file);
			
			//向文件中写入数据
			writer.write("Project Name: " + this.name + "\n\nSamples:\n");
			for (Sample sample : this.samples) {
				writer.write(sample.getName() + "\n");
			}
			
			writer.flush();
			writer.close();
			
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * 增加样品
	 * @param sample 样品对象
	 */
	public boolean addSample(Sample sample) {
		return this.samples.add(sample);
	}
	
	/**
	 * 删除样品
	 * @param index 样品在列表中的索引
	 */
	public Sample removeSample(int index) {
		return this.samples.remove(index);
	}
	
	/**
	 * 删除全部样品
	 * @param index 样品在列表中的索引
	 */
	public void removeAllSamples() {
		samples.clear();
		log.debug("Samples cleared, " + samples.size() + " samples remain.");
	}
	
	/**
	 * 获取项目名称
	 * @return 项目名称
	 */
	public String getName() {
		if (name == null) {
			name = "Untitled " + untitledIndex++;
		}
		return name;
	}

	/**
	 * 设置项目名称
	 * @param name 项目名称
	 */
	public void setName(String name) {
		this.name = name;
	}
	
	/**
	 * 获取项目包含的样品对象
	 * @return 样品对象的列表
	 */
	public List<Sample> getSamples() {
		return samples;
	}
	
	/**
	 * 设置项目包含的样品对象
	 * @param samples 样品对象的列表
	 */
	public void setSamples(List<Sample> samples) {
		this.samples = samples;
	}
	
	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}
	
	public Properties getProperties() {
		return properties;
	}

	public void setProperties(Properties properties) {
		this.properties = properties;
	}
	
	public boolean isStatusChanged() {
		return statusChanged;
	}

	public void setStatusChanged(boolean statusChanged) {
		this.statusChanged = statusChanged;
	}
}
